package com.kexio.security.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.Data;

/**
 * 用户注册请求DTO
 * 
 * @author kexio
 */
@Data
@Schema(description = "用户注册请求")
public class RegisterRequest {

    @NotBlank(message = "用户名不能为空")
    @Size(min = 3, max = 50, message = "用户名长度必须在3-50之间")
    @Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "用户名只能包含字母、数字和下划线")
    @Schema(description = "用户名", example = "johndoe")
    private String username;

    @NotBlank(message = "密码不能为空")
    @Size(min = 6, max = 100, message = "密码长度必须在6-100之间")
    @Schema(description = "密码", example = "123456")
    private String password;

    @NotBlank(message = "确认密码不能为空")
    @Schema(description = "确认密码", example = "123456")
    private String confirmPassword;

    @Email(message = "邮箱格式不正确")
    @Schema(description = "邮箱", example = "john@example.com")
    private String email;

    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
    @Schema(description = "手机号", example = "13800138000")
    private String phone;

    @Size(max = 50, message = "昵称长度不能超过50")
    @Schema(description = "昵称", example = "John")
    private String nickname;

    @Size(max = 50, message = "真实姓名长度不能超过50")
    @Schema(description = "真实姓名", example = "约翰·多伊")
    private String realName;

    @Schema(description = "验证码", example = "1234")
    private String captcha;

    @Schema(description = "邮箱验证码", example = "123456")
    private String emailCode;

    @Schema(description = "短信验证码", example = "123456")
    private String smsCode;

    @Schema(description = "邀请码", example = "INV12345")
    private String inviteCode;

    @Schema(description = "同意用户协议", example = "true")
    @AssertTrue(message = "必须同意用户协议")
    private Boolean agreeTerms;

    @Schema(description = "同意隐私政策", example = "true")
    @AssertTrue(message = "必须同意隐私政策")
    private Boolean agreePrivacy;

    // ==================== 手写Setter方法 ====================
    
    public void setUsername(String username) { this.username = username; }
    public void setPassword(String password) { this.password = password; }
    public void setConfirmPassword(String confirmPassword) { this.confirmPassword = confirmPassword; }
    public void setEmail(String email) { this.email = email; }
    public void setPhone(String phone) { this.phone = phone; }
    public void setNickname(String nickname) { this.nickname = nickname; }
    public void setRealName(String realName) { this.realName = realName; }
    public void setCaptcha(String captcha) { this.captcha = captcha; }
    public void setEmailCode(String emailCode) { this.emailCode = emailCode; }
    public void setSmsCode(String smsCode) { this.smsCode = smsCode; }
    public void setInviteCode(String inviteCode) { this.inviteCode = inviteCode; }
    public void setAgreeTerms(Boolean agreeTerms) { this.agreeTerms = agreeTerms; }
    public void setAgreePrivacy(Boolean agreePrivacy) { this.agreePrivacy = agreePrivacy; }

    // ==================== 便利方法 ====================

    /**
     * 验证密码是否匹配
     * @return 是否匹配
     */
    public boolean isPasswordMatch() {
        return password != null && password.equals(confirmPassword);
    }

    /**
     * 检查是否有邮箱验证码
     * @return 是否有邮箱验证码
     */
    public boolean hasEmailCode() {
        return emailCode != null && !emailCode.trim().isEmpty();
    }

    /**
     * 检查是否有短信验证码
     * @return 是否有短信验证码
     */
    public boolean hasSmsCode() {
        return smsCode != null && !smsCode.trim().isEmpty();
    }

    /**
     * 检查是否有邀请码
     * @return 是否有邀请码
     */
    public boolean hasInviteCode() {
        return inviteCode != null && !inviteCode.trim().isEmpty();
    }
}